package com.openbravo.pos.panels;

import java.util.List;

import javax.swing.JPanel;
import javax.swing.BoxLayout;
import com.openbravo.bean.ProductInfo;
import com.openbravo.bean.erp.OperationProduct;
import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.base.AppLocal;
import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.ui.swing.dnd.AbstractJBeanDropPanel;
import cn.ekuma.epos.db.table.erp.I_OperationProduct;

public class JDropProductByOperationCategoryPanel extends JPanel {

	private JPanel panel;
	I_DataLogic dlSales ;
	QBFParameters delQBFParameters;
	QBFParameter categoryParameter=new QBFParameter(I_OperationProduct.OPERATIONCATEGORIE);
	QBFParameter productParameter=new QBFParameter(I_OperationProduct.PRODUCT);
	/**
	 * Create the panel.
	 */
	public JDropProductByOperationCategoryPanel(final I_DataLogic dlSales2) {
		initComponents();
		this.dlSales=dlSales2;
		delQBFParameters=new QBFParameters();
		delQBFParameters.and(categoryParameter).and(productParameter);
		add(new AbstractJBeanDropPanel<OperationProductCategory,ProductInfo>(){
			@Override
			public void doDropInsert(List<ProductInfo> beans) {
				OperationProduct oProduct;
				
				for(ProductInfo p:beans){			
					oProduct=new OperationProduct();
					oProduct.setOperationCategoryId(bean.getID());
					oProduct.setProductId(p.getID());
					try {
					   dlSales.insert(oProduct);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}
			
			public void doDropDelete(List<ProductInfo> dropList) {
				categoryParameter.equal(bean.getID());
				for(ProductInfo p:dropList){
					productParameter.equal(p.getID());
					try {
					   dlSales.deleteByQBFParameters(OperationProduct.class,delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 

			@Override
			public String readerBean(OperationProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationProductCategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<OperationProductCategory,ProductInfo>(){
			@Override
			public void doDropInsert(List<ProductInfo> beans) {
				OperationProduct oProduct;
				
				for(ProductInfo p:beans){			
					oProduct=new OperationProduct();
					oProduct.setOperationCategoryId(bean.getID());
					oProduct.setProductId(p.getID());
					try {
					   dlSales.insert(oProduct);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}

			public void doDropDelete(List<ProductInfo> dropList) {
				categoryParameter.equal(bean.getID());
				for(ProductInfo p:dropList){
					productParameter.equal(p.getID());
					try {
					   dlSales.deleteByQBFParameters(OperationProduct.class,delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 
			
			@Override
			public String readerBean(OperationProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationProductCategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<OperationProductCategory,ProductInfo>(){
			@Override
			public void doDropInsert(List<ProductInfo> beans) {
				OperationProduct oProduct;
				
				for(ProductInfo p:beans){			
					oProduct=new OperationProduct();
					oProduct.setOperationCategoryId(bean.getID());
					oProduct.setProductId(p.getID());
					try {
					   dlSales.insert(oProduct);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}
			
			public void doDropDelete(List<ProductInfo> dropList) {
				categoryParameter.equal(bean.getID());
				for(ProductInfo p:dropList){
					productParameter.equal(p.getID());
					try {
					   dlSales.deleteByQBFParameters(OperationProduct.class,delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 
			
			@Override
			public String readerBean(OperationProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationProductCategory");
			}
			
		});
	}
	private void initComponents() {
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
	}
	
	public String getTabName(){
		return AppLocal.getIntString("label.operationProductCategory");
	}
}
